<template>
	<view class="zy-column-full">
		<zy-title rightText="保存" @right="requestSave"></zy-title>
		
		<scroll-view class="zy-scroll zy-ph-lg" scroll-y>
			<view class="zy-text-white-lg-bold zy-mt-lg">选择你的个性标签</view>
			<view class="zy-text-whitegrey zy-mt-sm">最多可选择10个</view>
			<zy-tag class="zy-mv-sm" close :list='selectTags'  @remove="removeTag"></zy-tag>
			<zy-line></zy-line>
			
			<zy-tab class="zy-mt-lg" hide-line :tabs="['关于我', '我的经历']" @change='changeTab'></zy-tab>
			<zy-tag class="zy-mv-sm" :list='tags'  @click="addTag"></zy-tag>
		</scroll-view>
	</view>
</template>

<script>
	import CacheUtil from '@/main/common/CacheUtil';
	import RouteUtil from '@/main/common/RouteUtil';
	export default {
		data() {
			return {
				userId: '',
				tagIds:'',
				selectTags:[],
				tags: [],
				tags1: [],
				tags2: []
			};
		},
		
		onLoad(option) {
			if(option){
				this.userId = CacheUtil.getUserId();
				this.tagIds = option.tagIds;
				this.requestDict();
			}
		},
		methods:{
			changeTab(index){
				this.tags = 0==index? this.tags1: this.tags2;
			},
			addTag(data){
				let flag = true;
				if(this.selectTags.length >= 10){
					uni.$toast.showToast('标签最多添加10个')
					return
				}
				this.selectTags.forEach(item=>{
					if(item.name == data.name){
						flag = false;
						uni.$toast.showToast('该标签已添加过')
					}
				});
				if(flag){
					this.selectTags.push(data);
				}
			},
			removeTag(data){
				this.selectTags = this.selectTags.filter(item=> item.name != data.name)
			},
			requestDict(){
				uni.$http.get("/api/dict/detail").then(res=>{
					let data = res.data.dictTagResult;
					let tagIdArray = [];
					if(this.tagIds){
						tagIdArray = this.tagIds.split(',')
					}
					data.dictTagHobbyResultList.forEach(item => {
						tagIdArray.forEach(tag => {
							if(item.id == tag){
								this.selectTags.push(item);
							}
						})
					})
					data.dictTagPersonalityResultList.forEach(item => {
						tagIdArray.forEach(tag => {
							if(item.id == tag){
								this.selectTags.push(item);
							}
						})
					})
					this.tags = this.tags1 = data.dictTagHobbyResultList;
					this.tags2 = data.dictTagPersonalityResultList;
				})
			},
			requestSave(){
				if(this.selectTags.length == 0){
					uni.$toast.showToast('请选择标签')
					return
				}
				let ids = '';
				this.selectTags.forEach(item=>{
					if('' == ids){
						ids = item.id;
					}else{
						ids += ','+item.id;
					}
				})
				let pararms = {tagIds: ids, id: this.userId}
				uni.$http.post("/api/user/updateUser", pararms).then(res=>{
					uni.$toast.showToast('添加成功');
					RouteUtil.back()
					uni.$emit('sendTags', {list: this.selectTags, tagIds: ids});
				})
			},
			onItemClick(item, index){
				item.isselect = !item.isselect;
			}
		}
	}
</script>

<style lang="scss">

</style>
